package com.go.where.manager.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.go.where.manager.common.Constants;
import com.go.where.manager.dao.FamilySubjectDao;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.page.BaseQuery.OrderType;
import com.go.where.manager.pojo.FamilySubjectInfo;
import com.go.where.manager.pojo.user.UserInfoVO;
import com.go.where.manager.service.FamilySubjectService;

@Service("familySubjectService")
public class FamilySubjectServiceImpl implements FamilySubjectService {

	@Resource(name = "familySubjectDao")
	private FamilySubjectDao familySubjectDao;

	@Override
	public FamilySubjectInfo queryByPrimartKey(String cid) {
		return familySubjectDao.getByKey(cid);
	}

	@Override
	public Pagination<FamilySubjectInfo> queryPage(FamilySubjectInfo subjectInfo) {
		if (StringUtils.isNotEmpty(subjectInfo.getOrderBy())
				&& subjectInfo.getOrderType() != null) {
			if (subjectInfo.getOrderType() == OrderType.asc) {
				subjectInfo.setPage_orderByAsc(subjectInfo.getOrderBy());

			} else {
				subjectInfo.setPage_orderByDesc(subjectInfo.getOrderBy());
			}
		}
		Integer count = familySubjectDao.totalCount(subjectInfo);
		subjectInfo.calculate();
		List<FamilySubjectInfo> list = familySubjectDao.queryList(subjectInfo);
		Pagination<FamilySubjectInfo> page = familySubjectDao.queryPagination(count, list,
				subjectInfo);
		return page;
	}

	@Override
	public int save(FamilySubjectInfo subjectInfo, UserInfoVO currentUser) {
		subjectInfo.setPublishTime(new Date());
		subjectInfo.setUserId(currentUser.getUserId());
		subjectInfo.setIsDelete(Constants.NOT_DELETED);
		return familySubjectDao.insert(subjectInfo);
	}

	@Override
	public int update(FamilySubjectInfo subjectInfo, UserInfoVO currentUser) {
		subjectInfo.setUpdateTime(new Date());
		subjectInfo.setUserId(currentUser.getUserId());
		return familySubjectDao.update(subjectInfo);
	}

}
